//
//  RetweetViewController.m
//  Guazi
//
//  Created by 李 电森 on 11-8-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "RetweetViewController.h"
#import "FriendGroup.h"
#import "TwitterClient.h"
#import "GuaziAppDelegate.h"

@interface RetweetViewController(private) 
- (void) loadGroups;
@end


@implementation RetweetViewController

@synthesize status;
@synthesize textView;
@synthesize toAllButton;
@synthesize toGroupButton;
@synthesize groupsTableView;


#pragma mark - init & destroy methods

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        groups = nil;
        selectedGroups = [[NSMutableDictionary alloc] init];
        toFlag = 0;
    }
    return self;
}


- (id) initWithStatus:(Status*)aStatus
{
    self = [super init];
    if ( self ) {
        self.status = aStatus;
        groups = nil;
        selectedGroups = [[NSMutableDictionary alloc] init];
        toFlag = 0;
    }
    return self;
}


- (void)dealloc
{
    [status release];
    [toAllButton release];
    [toGroupButton release];
    [textView release];
    [groupsTableView release];
    [groups release];
    [selectedGroups release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - View lifecycle

- (void) buildBarItems
{
    UIBarButtonItem* leftButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel)];
    self.navigationItem.leftBarButtonItem = leftButtonItem;
    [leftButtonItem release];
    
    UIBarButtonItem* rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"转发" style:UIBarButtonItemStyleDone target:self action:@selector(retweet)];
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    [self loadGroups];
    
    CGColorRef borderColor = [[UIColor colorWithRed:0.467f green:0.792f blue:0.914f alpha:1.0f] CGColor];
    self.textView.text = status.text;
    self.textView.layer.borderWidth = 1.0;
    self.textView.layer.borderColor = borderColor;
    
    self.groupsTableView.dataSource = self;
    self.groupsTableView.delegate = self;

    self.groupsTableView.layer.masksToBounds=YES;
    self.groupsTableView.layer.cornerRadius=8.0;
    self.groupsTableView.layer.borderWidth=1.0;
    self.groupsTableView.layer.borderColor=borderColor; 
    
    [self buildBarItems];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.status  = nil;
    self.textView = nil;
    self.toAllButton = nil;
    self.toGroupButton = nil;
    self.groupsTableView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - TableView Datasource

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [groups count];
}


- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendGroup* group = [groups objectAtIndex:indexPath.row];
    static NSString* s = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:s];
    if ( !cell ) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:s];
        cell.backgroundColor = tableView.backgroundColor;
        cell.textLabel.font = [UIFont systemFontOfSize:14];
    }
    
    cell.textLabel.text = group.name;
    return cell;
}


#pragma mark - TableView Delegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
    if ( cell ) {
        FriendGroup* group = [groups objectAtIndex:indexPath.row];
        if ( cell.accessoryType == UITableViewCellAccessoryCheckmark ) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [selectedGroups removeObjectForKey:[NSNumber numberWithLongLong:group.groupId]];
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            [selectedGroups setObject:group forKey:[NSNumber numberWithLongLong:group.groupId]];
        }
        
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
}


#pragma mark - load data

- (void) loadGroups
{
    NSArray* arr = [FriendGroup loadGroups:NO onlyRealGroup:YES];
    groups = [[NSMutableArray alloc] initWithArray:arr];
}


#pragma mark - tool methods

- (IBAction) toAll
{
    [toAllButton setImage:[UIImage imageNamed:@"favorited.png"] forState:UIControlStateNormal];
    [toGroupButton setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
    [groupsTableView setHidden:YES];
    toFlag = 1;
}


- (IBAction) toGroups
{
    [toAllButton setImage:[UIImage imageNamed:@"favorite.png"] forState:UIControlStateNormal];
    [toGroupButton setImage:[UIImage imageNamed:@"favorited.png"] forState:UIControlStateNormal];
    [groupsTableView setHidden:NO];
    toFlag = 2;
    
}


- (void) retweet
{
    if ( toFlag == 0 ) {
        [GuaziAppDelegate showAlert:@"请选择分享给所有人或者分享给分组！"];
        return;
    }
    else if ( toFlag == 2 ) {
        if ( [selectedGroups count] == 0 ) {
            [GuaziAppDelegate showAlert:@"请选择要分享的分组！"];
            return;
         }
    }
    
    [TwitterClient retweetSync:status];
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] dismissModalViewControllerAnimated:YES];
}


- (void) cancel
{
    [[[GuaziAppDelegate getAppDelegate] currentNavigationController] dismissModalViewControllerAnimated:YES];
}



@end
